﻿
var mSystemParameters = [];

$(function () {


    setAuthority();

    $('#systemPg').propertygrid({
        showGroup: true,
        scrollbarSize: 0,
        fitColumns: true,
        columns: [[
              { title: '名称', field: 'name', width: 200 },
              { title: '值', field: 'value', width: 150 }

        ]]
    });

    loadSystemSystemParameters();
    display();
    $("#btnSave").bind("click", SaveOrUpDateParameter);
});


function setAuthority() {
    $.ajax({
        url: "/System/CheckAuthority",
        type: "POST",
        async: false,
        dataType: "json",
        data: { "roleKey": 'admin' },
        success: function (req) {
            if (req.IsSuccess) {
                $('#btnSave').show();
            } else {
                $('#btnSave').hide();
            }

            if (req.Data) {
                $.messager.alert('提示', req.Data);
            }
        },
    });
}

function loadSystemSystemParameters() {
    $.ajax({
        url: "/System/LoadSystemParameterList",
        type: "POST",
        async: false,
        dataType: "json",
        success: function (req) {
            if (req.IsSuccess) {
                var data = JSON.parse(req.Data);
                convert(data);

            } else {
                $.messager.alert('提示', '加载系统参数失败!' + req.Data);
            }
        },
    });
}
//转换到前台
function convert(data) {
    var count = data.length;

    for (var i = 0; i < count; i++) {
        var sp = {};
        var d = data[i];
        sp.id = d.Id;
        sp.name = d.Name;
        sp.group = d.GroupName;
        sp.value = d.Value;
        sp.editor = d.Editor;
        sp.key = d.FKey;
        mSystemParameters.push(sp);
    }
}

function display() {
    $('#systemPg').propertygrid('loadData', mSystemParameters);
}

function SaveOrUpDateParameter() {
    try {
        $('#div_systemPg').mask('正在保存物品记录单据...');
        var datas = $("#systemPg").propertygrid("getData");
        var retstr = CheckValueEffect(datas.rows);
        if (retstr == "TRUE") {
            var SystemParameter = [];
            for (var i = 0; i < datas.rows.length; i++) {
                var Parameter = {};
                Parameter.Id = datas.rows[i].id;
                Parameter.FKey = datas.rows[i].key;
                Parameter.Name = datas.rows[i].name;
                Parameter.GroupName = datas.rows[i].group;
                Parameter.Value = datas.rows[i].value;
                Parameter.Editor = datas.rows[i].editor;
                SystemParameter.push(Parameter);
            }
            var objToStr = JSON.stringify(SystemParameter);
            $.ajax({
                url: "/System/SaveSystemParameterList",
                type: "POST",
                dataType: "json",
                data: { "SystemParameters": objToStr },
                async: false,
                success: function (req) {
                    if (req.IsSuccess) {
                        $.messager.alert('提示', '保存成功!');
                    }
                    $('#div_systemPg').unmask();
                },
            });
        } else {
            $.messager.alert("操作提示", retstr, "error");
            $('#div_systemPg').unmask();
            return;
        }
    } catch (ex) {
        $.messager.alert(ex);
        $('#div_systemPg').unmask();
    } finally {
        $('#div_systemPg').unmask();
    }
};
function CheckValueEffect(data) {
    var dTime = data[0].value;
    if (!$.isNumeric(dTime)) {
        return "入库期限，请输入整数数字";
    }
    if (dTime < 12) {
        return "入库期限时间必须大于12小时";
    }
    if (dTime > 72) {
        return "入库期限时间必须小于72小时";
    }
    var mTime = data[1].value;
    if (!$.isNumeric(mTime)) {
        return "入库提示时间，请输入整数数字";
    }
    if (mTime < 2) {
        return "入库提示时间必须大于两小时";
    }
    if (parseInt(mTime) > parseInt(dTime)) {
        return "入库提示时间必须小于入库期限时间";
    }
    return "TRUE";
};